/**
 * TMP001新增
 * */


var formArys = {
    title: '',
    imgs: [],
    size_c: 0,
    size_k: 0,
    size_g: 0,
}; //表单对象


//新增表单
function formAdd(){
    //if (!vid) {
    //    msgPop({msg: '没有视频ID'}); return
    //}
    var h = '';
    h += '<style type="text/css">.sel-list { width: 100%; max-height: 160px; border: 1px solid #0088CC; overflow: auto } .sel-option { margin-bottom: 2px; padding: 5px 0; width: 100%; text-align: center; background: #efefef; } .group .abtn { margin-top: 5px; padding: 5px 0; width: 100%; text-align: center; background: #0088CC; color: #fff; border-radius: 5px; } #selCate { font-weight: normal; float: right; color: #888; }</style>';
    //图片
    h += '<div class="title">箱体外观上传';
    h += '<div id="uploadOut" style="margin: 10px; width: 90%; height: 35px; background: red; overflow: hidden;">';
    h += uploadHtml();
    h += '</div>';
    h += '</div>';
    h += '<div class="group" id="imgs">';
    h += '</div>';
    //收纳名称
    h += '<div class="title">收纳名称</div>';
    h += '<div class="group">';
    h += '<input type="text" placeholder="输入收纳名" onblur="formArys.title=this.value;"/>';
    h += '</div>';
    //储物类型
    //h += '<div class="title">储物类型 <span id="selCate">000</span></div>';
    h += '<div class="title">储物类型</div>';
    h += '<div class="group">';
    //h += '<div class="sel-list">';
    //h += '<div class="sel-option">衣服-衬衣</div>';
    //h += '<div class="sel-option">鞋子-皮鞋</div>';
    //h += '<div class="sel-option">衣服-衬衣</div>';
    //h += '<div class="sel-option">鞋子-皮鞋</div>';
    //h += '<div class="sel-option">衣服-衬衣</div>';
    //h += '<div class="sel-option">鞋子-皮鞋</div>';
    //h += '</div>';
    //h += '<div class="abtn">新增类型</div>';
    //h += '</div>';
    h += '<span style="color: #888;">保存后再编辑</span>';
    h += '</div>';
    //箱体长
    h += '<div class="title">箱体长（毫米MM）</div>';
    h += '<div class="group">';
    h += '<input type="number" placeholder="输入箱体长" onblur="formArys.size_c=this.value;"/>';
    h += '</div>';
    //箱体宽
    h += '<div class="title">箱体宽（毫米MM）</div>';
    h += '<div class="group">';
    h += '<input type="number" placeholder="输入箱体宽" onblur="formArys.size_k=this.value;"/>';
    h += '</div>';
    //箱体高
    h += '<div class="title">箱体高（毫米MM）</div>';
    h += '<div class="group">';
    h += '<input type="number" placeholder="输入箱体高" onblur="formArys.size_g=this.value;"/>';
    h += '</div>';
    //箱体高
    h += '<div class="title">储物内容</div>';
    h += '<div class="group">';
    h += '<span style="color: #888;">保存后再编辑</span>';
    h += '</div>';
    //按钮
    h += '<div id="tb-btn" onclick="formSave();">保存</div>';
    $('#form').html(h);
}


//上传HTML
function uploadHtml(){
    var h = '';
    h += '<div style="padding: 10px 5%; width: 90%; background: #555; text-align: center; font-size: 12px; color: #fff; position: relative;">上传图片</div>';
    h += '<input type="file" id="uploadFile" style="width: 100%; height: 35px; opacity: 0; position: relative; top: -36px;" onchange="uploadImage()"/>';
    return h;
}


//图片HTML
function imgHtml(){
    var h = '';
    for (var i = 0; i < formArys.imgs.length; i ++) {
        h += '<div style="margin: 5px auto; width: 96%; text-align: center; overflow: hidden;">';
        h += '<img src="' + formArys.imgs[i] + '" style="width: 96%; border: 1px solid #555;"/>';
        h += '<a href="javascript:;" style="padding: 5px 5%; width: 90%; color: #fff; background: red; font-size: 12px; border: 0; border-radius: 0; opacity: 0.7; position: relative; top: -30px;" onclick="imgDel(' + i + ');">点击删除</a>';
        h += '</div>';
    }
    return h;
}


//显示图片
function showImg(e){
    if (formArys.imgs.length >= 9) {
        msgPop({msg: '最多上传9个图'}); return
    }
    formArys.imgs.push(e);
    //console.log(formArys.imgs);
    $('#imgs').html(imgHtml());
    $('#uploadOut').html(uploadHtml());
}


//删除图片
function imgDel(v){
    var newImgs = [];
    for (var i = 0; i < formArys.imgs.length; i ++) {
        if (i != v) {
            newImgs.push(formArys.imgs[i]);
        }
    }
    formArys.imgs = newImgs;
    $('#imgs').html(imgHtml());
}


//保存表单
function formSave(){
    if (token == '') {
        msgPop({msg: '没有登录'});
        window.location.href = '/login.html?code=' + code;
        return
    }
    if (formArys.title == '') {
        msgPop({msg: '收纳名称必须的'}); return
    }
    if (formArys.imgs.length == 0) {
        msgPop({msg: '外观图片必须的'}); return
    }
    var formData = formArys;
    formData.code = code;
    formData.token = token;
    formData.videoId = vid;
    //console.log(formData); return
    $.ajax({
        type: apis.tmp2Add.method,
        url: apis.tmp2Add.url,
        data: formArys,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            window.location.href = routes.tmp2Index + '?code=' + code + '&token=' + token;
        },
        error: function () {
            console.log('网络错误');
        }
    });
}